//package cn.hznc.mqtt;
//
//import cn.nerc.nclink.core.general.InvalidException;
//        import com.alibaba.fastjson.JSON;
//
//        import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//        import org.eclipse.paho.client.mqttv3.MqttException;
//        import org.eclipse.paho.client.mqttv3.MqttMessage;
//        import org.springframework.beans.factory.annotation.Autowired;
//        import org.springframework.beans.factory.annotation.Value;
//        import org.springframework.context.annotation.Bean;
//        import org.springframework.context.annotation.Configuration;
//        import org.springframework.context.annotation.Lazy;
//        import org.springframework.integration.annotation.IntegrationComponentScan;
//        import org.springframework.integration.annotation.ServiceActivator;
//        import org.springframework.integration.channel.DirectChannel;
//        import org.springframework.integration.core.MessageProducer;
//        import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
//        import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
//        import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
//        import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
//        import org.springframework.messaging.MessageChannel;
//        import org.springframework.messaging.MessageHandler;
//
//        import java.awt.*;
//        import java.text.ParseException;
//        import java.util.List;
//        import java.util.concurrent.TimeoutException;
//
//
//@Configuration
//@IntegrationComponentScan
//public class MqttRecieveConfig {
//
//    @Autowired
//    private  MqttService mqttService;
//
////    @Lazy
////    @Autowired(required = false)
////    private CloudEdgeService cloudEdgeService;
//
//    @Value("${spring.mqtt.username}")
//    private String username;
//
//    @Value("${spring.mqtt.password}")
//    private String password;
//
//    @Value("${spring.mqtt.url}")
//    private String hostUrl;
//
//    @Value("${spring.mqtt.client.id}")
//    private String clientId;
//
//    @Value("${spring.mqtt.default.topic}")
//    private String defaultTopic;
//
//    private MqttPahoMessageDrivenChannelAdapter adapter;
//
//    @Bean
//    public MqttConnectOptions getMqttConnectOptions_in() {
//        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
//        mqttConnectOptions.setUserName(username);
//        mqttConnectOptions.setPassword(password.toCharArray());
//        mqttConnectOptions.setServerURIs(new String[]{hostUrl});
//        mqttConnectOptions.setKeepAliveInterval(2);
//        return mqttConnectOptions;
//    }
//
//    @Bean
//    public MqttPahoClientFactory mqttClientFactory_in() {
//        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
//        factory.setConnectionOptions(getMqttConnectOptions_in());
//        return factory;
//    }
//
//    //接收通道
//    @Bean
//    public MessageChannel mqttInputChannel() {
//        return new DirectChannel();
//    }
//
//    //客户端初始化，适配器
//    @Bean
//    public MessageProducer inbound() {
//        /*MqttPahoMessageDrivenChannelAdapter adapter =
//                new MqttPahoMessageDrivenChannelAdapter(clientId+"_inbound", mqttClientFactory_in(),
//                        "Discovery/Response","Probe/Query/Response/#","Query/Response/#","Register/Response","Probe/Version/#", "MQTT/Test/#");*/
//        adapter =
//                new MqttPahoMessageDrivenChannelAdapter(clientId+"_inbound", mqttClientFactory_in(),
//                        "Discovery/Response","Probe/Query/Response/#","Query/Response/#","Register/Request","Probe/Version/#","Sample/+/sample_channel0");
////        adapter = new MqttPahoMessageDrivenChannelAdapter(clientId+"_inbound", mqttClientFactory_in());
//        adapter.setCompletionTimeout(10000);
//        adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setQos(1);
//        adapter.setOutputChannel(mqttInputChannel());
//        //adapter.addTopic("Probe/Query/Response/#");
//        //adapter.addTopic("Query/Response/#");
//        //adapter.addTopic("Register/Response");
//        // adapter.addTopic("Probe/Version/#");
//        return adapter;
//    }
//
//    //增加单个订阅主题
//    public void addAdapterTopic(String topic) {
//        if(null == this.adapter) {
//            adapter = new MqttPahoMessageDrivenChannelAdapter(clientId+"_inbound", mqttClientFactory_in());
//            adapter.setCompletionTimeout(10000);
//            adapter.setConverter(new DefaultPahoMessageConverter());
//            adapter.setQos(1);
//            adapter.setOutputChannel(mqttInputChannel());
//        }
//        this.adapter.addTopic(topic);
//    }
//
//    //批量增加订阅主题
//    public void addAdapterTopicList(List<String> topicList) {
//        if(null == this.adapter) {
//            adapter = new MqttPahoMessageDrivenChannelAdapter(clientId+"_inbound", mqttClientFactory_in());
//            adapter.setCompletionTimeout(10000);
//            adapter.setConverter(new DefaultPahoMessageConverter());
//            adapter.setQos(1);
//            adapter.setOutputChannel(mqttInputChannel());
//        }
//        for (String topic : topicList) {
//            adapter.addTopic(topic);
//        }
//    }
//
//    //删除单个订阅主题
//    public void deleteAdapterTopic(String topic) {
//        if(null == adapter) {
//            return;
//        }
//        adapter.removeTopic(topic);
//    }
//
//    //批量删除订阅主题
//    public void deleteAdapterTopicList(List<String> topicList) {
//        if(null == adapter) {
//            return;
//        }
//        for (String topic : topicList) {
//            adapter.removeTopic(topic);
//        }
//    }
//
//    public String getUsername_test(){
//        return  username;
//    }
//
//
//    //通过通道获取数据
//    @Bean
//    @ServiceActivator(inputChannel = "mqttInputChannel")
//    public MessageHandler handler() {
//        return message -> {
//            try {
//                mqttService.handle_message(message.getHeaders().get("mqtt_receivedTopic").toString(),
//                        JSON.parseObject(message.getPayload().toString()));
//
//            } catch (ParseException e) {
//                e.printStackTrace();
//            } catch (TimeoutException e) {
//                e.printStackTrace();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (InvalidException e) {
//                e.printStackTrace();
//            } catch (MqttException e) {
//                e.printStackTrace();
//            }
//        };
//    }
//}
